<template>
	<div class="check">
		<div class="have" v-show="yesNo">
			<div class="btnbox clear">
				<el-button type="primary" size="small" @click="seek">{{seekText}}</el-button>
				<el-button type="danger" size="small" @click="leave">离开</el-button>
			</div>
			<el-tabs v-model="activeName" @tab-click="handleClick">
			    <el-tab-pane :label="tabFirst" name="first"></el-tab-pane>
			    <el-tab-pane :label="tabSec" name="second"></el-tab-pane>
			    <el-tab-pane :label="tabThird" name="third"></el-tab-pane>
			    <el-tab-pane :label="tabFourth" name="fourth"></el-tab-pane>
			</el-tabs>
			<Bluetable
				:tHead="tHead"
				:tData="tableData"
				ref="blueTable" @operateClick="operateClick">
			</Bluetable>
			<pagination
				:total="query.total"
				:page.sync="query.page"
				:limit.sync="query.limit"
				@pagination="getList"/>
		</div>
		<div class="no" v-show="!yesNo">
			<el-button type="warning" round class="open_btn" @click="openYes">开启门诊监测<i class="el-icon-arrow-right el-icon--right"></i></el-button>
		</div>
		<Queryseekpop :seekRow="seekRow" ref="qPop" type="mz" @clickFun="clickFun"></Queryseekpop>
		<HistoryVersion :vRow="vRow" ref="qvPop"></HistoryVersion>
	</div>
</template>

<script>
	import Queryseekpop from "../components/querySeekPop"
	import HistoryVersion from "../components/historyVersion"
	export default {
		name:"query",
		components:{Queryseekpop,HistoryVersion},
		data () {
			return {
				audio:null,
				seekText:"查询进行中",
				seekFlag:true,
				activeName:"first",
				query: {
				  total: 0,
				  page: 1,
				  limit: 10
				},
				tHead: [
				  {
				    label: "操作",
				    width: "150px",
				    operate: ["查看"]
				  },
				  {
				    prop: "user_name",
				    label: "新增时间"
				  },
				  {
				    prop: "real_name",
				    label: "审核状态"
				  },
				  {
				    prop: "state_flag",
				    label: "加急状态"
				  },
				  {
				    prop: "deptNames",
				    label: "门诊/急诊/住院号",
					width:"150px",
				  },
				  {
				    prop: "create_time",
				    label: "病人姓名"
				  },
				  {
				    prop: "create_username",
				    label: "医生姓名"
				  },
				  {
				    prop: "create_username",
				    label: "科室名称"
				  },
				  {
				    prop: "create_username",
				    label: "审查警示级别"
				  },
				  {
				    prop: "create_username",
				    label: "倒计时"
				  },
				],
				tableData: [],
				socket:"",
				seekRow:{},
				vRow:{},
				yesNo:false,
				tabFirst:"",
				tabSec:"",
				tabThird:"",
				tabFourth:"",
			}
		},
		mounted () {
			this.setThead();
			this.getList();
			this.getNum();
			this.setAudio();
		},
		methods:{
			setAudio() {
			  this.audio = new Audio()
			  this.audio.src = require('@/img/msgtip.mp3');
			},
			playFun() {
			  this.audio.play()
			},
			pauseFun() {
			  this.audio.pause()
			},
			leave () {
				this.yesNo = false;
				this.close();
			},
			async getNum () {
				let res = await this.$ptc.orderallCount({
					type:"mz"
				})
				if(res.code == 1){
					this.tabFirst = "待审核处方（"+res.data.noExamCount+"）";
					this.tabSec = "待医生双签处方（"+res.data.waitDoctorCount+"）";
					this.tabThird = "已拒绝处方（"+res.data.pharPassCount+"）";
					this.tabFourth = "已审核处方（"+res.data.phar_state+"）";
				}
				
			},
			async openYes () {
				let res = await this.$ptc.panduanDept({
					type:"mz"
				})
				console.log(res)
				if(res.code == 200){
					this.intWebsocket();
				}
			},
			intWebsocket () {
				if(typeof(WebSocket) === "undefined"){
					alert("您的浏览器不支持socket")
				}else{
					// 实例化socket
					this.socket = new WebSocket("ws://yuwenbincl.xicp.net:13333")
					// 监听socket连接
					this.socket.onopen = this.open
					// 监听socket错误信息
					this.socket.onerror = this.error
					// 监听socket消息
					this.socket.onmessage = this.getMessage
				}
			},
			open () {
				console.log("socket连接成功")
				this.send();
			},
			error () {
				console.log("连接错误")
				this.intWebsocket();
			},
			getMessage (msg) {
				let data = JSON.parse(msg.data);
				console.log(data,111)
				if(data.status == 200){
					if(data.content == "newMz"){
						this.playFun();
						this.getNum();
						this.getList();
					}
				}
			},
			send () {
				var user = {
					userId:"lisi_mz",
					type:"REGISTER"
				}
				this.socket.send(JSON.stringify(user));
			},
			close () {
				console.log("socket已经关闭",111)
			},
			handleClick(tab, event) {
				this.activeName=tab.name;this.setThead();
				this.query={
					page:1,
					limit:10,
					total:0
				}
				this.tableData=[]
				this.getList();
			},
			setThead(){
				if(this.activeName == "first"){
					this.tHead = [
						{
						  label: "操作",
						  width: "150px",
						  operate: ["查看"]
						},
						{
						  prop: "add_time",
						  label: "新增时间"
						},
						{
						  prop: "phar_state_txt",
						  label: "审核状态"
						},
						{
						  prop: "urgent",
						  label: "加急状态"
						},
						{
						  prop: "mz_jz_zyh",
						  label: "门诊/急诊/住院号",
						  width:"150px",
						},
						{
						  prop: "patientName",
						  label: "病人姓名"
						},
						{
						  prop: "doctorName",
						  label: "医生姓名"
						},
						{
						  prop: "deptName",
						  label: "科室名称"
						},
						{
						  prop: "slCode_txt",
						  label: "审查警示级别"
						},
						{
						  prop: "time",
						  label: "倒计时"
						},
					]
				}else if(this.activeName == "second"){
					this.tHead = [
						{
						  label: "操作",
						  width: "150px",
						  operate: ["查看"]
						},
						{
						  prop: "add_time",
						  label: "新增时间"
						},
						{
						  prop: "phar_state_txt",
						  label: "审核状态"
						},
						{
						  prop: "urgent",
						  label: "加急状态"
						},
						{
						  prop: "mz_jz_zyh",
						  label: "门诊/急诊/住院号",
						  width:"150px",
						},
						{
						  prop: "patientName",
						  label: "病人姓名"
						},
						{
						  prop: "doctorName",
						  label: "医生姓名"
						},
						{
						  prop: "deptName",
						  label: "科室名称"
						},
						{
						  prop: "slCode_txt",
						  label: "审查警示级别"
						},
					]
				}else if(this.activeName == "third"){
					this.tHead = [
						{
						  label: "操作",
						  width: "150px",
						  operate: ["查看"]
						},
						{
						  prop: "add_time",
						  label: "新增时间"
						},
						{
						  prop: "real_name",
						  label: "审查时间"
						},
						{
						  prop: "mz_jz_zyh",
						  label: "门诊/急诊/住院号",
						  width:"150px",
						},
						{
						  prop: "patientName",
						  label: "病人姓名"
						},
						{
						  prop: "doctorName",
						  label: "医生姓名"
						},
						{
						  prop: "deptName",
						  label: "科室名称"
						},
						{
						  prop: "slCode_txt",
						  label: "审查警示级别"
						},
					]
				}else if(this.activeName == "fourth"){
					this.tHead = [
						{
						  label: "操作",
						  width: "150px",
						  operate: ["查看"]
						},
						{
						  prop: "add_time",
						  label: "新增时间"
						},
						{
						  prop: "real_name",
						  label: "审查时间"
						},
						{
						  prop: "phar_state_txt",
						  label: "审核状态"
						},
						{
						  prop: "mz_jz_zyh",
						  label: "门诊/急诊/住院号",
						  width:"150px",
						},
						{
						  prop: "patientName",
						  label: "病人姓名"
						},
						{
						  prop: "doctorName",
						  label: "医生姓名"
						},
						{
						  prop: "deptName",
						  label: "科室名称"
						},
						{
						  prop: "slCode_txt",
						  label: "审查警示级别"
						},
					]
				}
			},
			async getList(){
				let res;
				if(this.activeName == "first"){
					 res=await this.$ptc.noexamOrderJson({
						 type:'mz',
						 page: this.query.page,
						 pageSize:this.query.limit
					 })
				}else if(this.activeName == "second"){
					 res=await this.$ptc.waitDoctorDouble({
						 type:'mz',
						 page: this.query.page,
						 pageSize:this.query.limit
					 })
				}else if(this.activeName == "third"){
					 res=await this.$ptc.refuseOrderJson({
						 type:'mz',
						 page: this.query.page,
						 pageSize:this.query.limit
					 })
				}else if(this.activeName == "fourth"){
					 res=await this.$ptc.pharPassOrderJson({
						 type:'mz',
						 page: this.query.page,
						 pageSize:this.query.limit
					 })
				}
				for(var i=0;i<res.rows.length;i++){
					if(res.rows[i].add_time){
						res.rows[i].add_time=this.$utils.timeFormat(res.rows[i].add_time)
					}
					if(res.rows[i].real_name){
						res.rows[i].real_name=this.$utils.timeFormat(res.rows[i].real_name)
					}
				}
				this.tableData=res.rows;
				this.query.total=res.total;
			},
			seek () {
				let flag = this.seekFlag;
				if(flag){
					this.seekFlag = false;
					this.seekText = "查询停止";
					this.socket.onclose = this.close;
				}else{
					this.seekFlag = true;
					this.seekText = "查询进行中";
					this.socket.onopen = this.open
				}
			},
			async operateClick (val,row) {
				console.log(row)
				row.id=row.patInfoId;
				this.seekRow = row;
				this.$refs.qPop.show();
			},
			clickFun(row){
				this.vRow = row;
				this.$refs.qvPop.show();
			}
		},
		destroyed () {
			this.socket.onclose = this.close;
		}
	}
</script>

<style lang="scss" scoped>
	.have{
		padding:20px;
		position:relative;
		.btnbox{
			position:absolute;
			top:14px;
			right:20px;
			z-index:999;
		}
	}
	.no{
		width:100%;
		height:calc(100vh - 95px);
		background:url(../../../img/nopower.jpg) no-repeat center;
		background-size:100% 100%;
		position:relative;
		.open_btn{
			height:40px;
			position:absolute;
			left:0;
			right:0;
			top:0;
			bottom:0;
			margin:auto;
		}
	}
</style>
